﻿
@{
    ViewBag.Title = "TypeDY";
    Layout = "~/Views/Shared/_BCLayout.cshtml";
}

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>综艺分类</legend>
</fieldset>
<form class="layui-form">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="标题关键字" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">主题选择</label>
        <div class="layui-input-inline" style="z-index:5;width:500px;">
            <select id="SubjectSel" lay-verify="required" lay-search>
                <option value=""></option>
                @for (int i = 0; i < ViewBag.SubjectList.Count; i++)
                {

                    <option value="@ViewBag.SubjectList[i]["SubjectId"]">@ViewBag.SubjectList[i]["Title"]</option>
                }
            </select>
        </div>
        <button type="button" class="layui-btn" onclick="addSubject()">添加</button>
    </div>
</form>


<table id="demo" lay-filter="SubjectList"></table>

<script type="text/javascript">
    var table;
    layui.use(['table', 'form', 'element'], function () {
        table = layui.table;
        var tab = parent.tab;
        var form = layui.form;
        form.render();

        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/Home/TypeZYTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "TypeZYId", Order: "Desc", Condition: " and 1=1" }
            , limits: [50, 100, 200]
            , limit: 50
            , cols: [[ //表头
                { field: 'Title', title: '标题', width: 600 }
                //, { field: 'Title3', title: '标题3', width: 200 }
                , {
                    field: "operation", title: "操作", width: 200,
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="Delete">删除</a>'
                    }
                }
            ]]
        });

        table.on("tool(SubjectList)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            //GetArticle
            if (layEvent == 'Delete') {
                //alert(data.ArticleId);
                $.ajax({
                    type: "POST",
                    url: "/Home/API_TypeZYDel",
                    async: false,
                    data: { "TypeZYId": data.TypeZYId },
                    success: function (msg) {
                        if (msg.result == "OK") {
                            table.reload("demo");
                        } else {
                            alert("删除失败");
                        }
                    }
                });
            }

        });



    });


    function addSubject() {
        var SubjectSel = "";
        SubjectSel = $("#SubjectSel").val();
        if (SubjectSel == "") {
            return;
        }
        $.ajax({
            type: "POST",
            url: "/Home/API_TypeZYAdd",
            async: false,
            data: { "SubjectId": SubjectSel },
            success: function (msg) {
                if (msg.result == "OK") {
                    table.reload("demo", {
                        //where: { SortName: "SSlistId", Order: "Desc", Condition: " and 1=1" }
                    });

                } else {
                    alert("新增失败");
                }
            }
        });


    }

    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }


    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo", {
                where: { SortName: "SubjectId", Order: "Desc", Condition: " and 1=1" }
                , page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "SubjectId", Order: "Desc", Condition: " and Title like '%" + text + "%'"
                }
                , page: {
                    curr: 1
                }
            });
        }
    }



</script>

